import OpenOPC
import time


def opc_da_client():
    try:
        # 创建 OPC 客户端实例
        opc = OpenOPC.client()

        # 获取可用的 OPC 服务器列表
        servers = opc.servers()
        print(f"Available OPC servers: {servers}")

        # 连接到指定的 OPC 服务器
        opc.connect('ICSvr1.0')

        # 读取变量 ICS1 的值
        while True:
            try:
                value = opc.read('ICS1')
                print(f"Variable ICS1: {value}")
            except Exception as read_error:
                print(f"Error while reading data: {read_error}")
            time.sleep(1)

    except Exception as connect_error:
        print(f"Failed to connect to the OPC DA server: {connect_error}")
    finally:
        if 'opc' in locals():
            opc.close()


if __name__ == "__main__":
    opc_da_client()
    